

//simefun 不能继承任何对象！！！重要
import (TaskInput) in "task/input";


// 预定义的中文名字和姓氏列表
string *chineseFirstNames = ([
    "子涵", "宇轩", "欣怡", "晨曦", "梓涵", "诗涵",
    "浩然", "雨婷", "嘉诚", "思琪", "俊轩", "梦洁",
    "子淳", "子豪", "梓萱", "思涵", "梓涵", "若兮",
    "晨浩", "清扬", "恒古", "老五", "老四", "老三",
    "子贡", "子清", "子淳", "老六", "鹏来", "老二","东来","西来","北城"
]);

string *chineseLastNames = ([
    "李", "王", "张", "刘", "陈", "杨",
    "赵", "黄", "周", "吴", "徐", "孙",
    "胡", "朱", "高", "林", "何", "郭",
    "马", "罗", "梁", "宋", "郑", "谢",
    "韩", "唐", "冯", "于", "董", "萧",
    "程", "曹", "袁", "邓", "许", "傅",
    "沈", "曾", "彭", "吕", "苏", "卢",
    "蒋", "蔡", "贾", "丁", "魏", "薛",
    "叶", "阎", "余", "潘", "杜", "戴",
    "夏", "钟", "汪", "田", "任", "姜",
    "范", "方", "石", "姚", "谭", "廖",
    "邹", "熊", "金", "陆", "郝", "孔",
    "白", "崔", "康", "毛", "邱", "秦",
    "江", "史", "顾", "侯", "邵", "孟",
    "龙", "万", "段", "漕", "钱", "汤",
    "尹", "黎", "易", "常", "武", "乔",
    "贺", "赖", "龚", "文"
]);



// 生成随机索引
int  getRandomIndex( var arr) {
    return random(0,arr.size());
}

// 生成随机中文姓名
string randomCnName() {
    int randomLastNameIndex = getRandomIndex(this.chineseLastNames);
    int randomFirstNameIndex = getRandomIndex(this.chineseFirstNames);
    string lastName = this.chineseLastNames[randomLastNameIndex];
    string firstName = this.chineseFirstNames[randomFirstNameIndex];
    return lastName+firstName;
}



//移除后缀
string rmSuffix(string file){
    int pos = file.lastIndexOf(".");
    println("位置"+pos);
    file = file.subString(0,pos);
    return file;
}


/*
   1、对象池中找
   2、找不到就new
*/
//var loadObject(string file){
//    var c = "task/input";
//    var e = newObj(file);
//    c::allObjs.add(e);
//    return e;
//}


var new(string file){
    try{
         var e = newObj(file);
      //调用create方法
       if( funcExists(e,"create") ){
           e.create();
       }
        //调用load方法
      if( funcExists(e,"load") ){
          e.load();
      }

      //设置文件地址 要求所有的类都要继承object，才能有set方法
      if( funcExists(e,"set") ){
         e.set("file_addr",file + "#"+hashCode(e));
      }else{
        println(file + "没有set方法!");
      }

        TaskInput::allObjs.put(hashCode(e),e);
        return e;
    }catch(err){
        println(file + "创建失败！");
        return 0;
    }
}

//获取所有已经加载的对象
var objects(){
   return TaskInput::allObjs;
}

int userp(var obj){
   if( funcExists(obj,"isUser") && obj.isUser() ){
         return true;
   }
   return false;
}
//在已加载的类实例中中查找需要的实例
object D(string clsName){
    var data = filterMap(TaskInput::allObjs,(k,v)->{
//         println("对象:"+baseName(v));
          if( baseName(v)==clsName ) return true;
    });
    //只要data不是0、none false 就是true

    if( data ) return data;
    return 0;
}
//类似于findObject
object E(string fileAddr){
   var data = filterMap(TaskInput::allObjs,(k,v)->{
      if( funcExists(v,"query") && v.query("file_addr") == fileAddr ) return true;
   });
   if( data ) return data;
   return 0;
}

object findObjectHash(string hash){

  var data = filterMap(TaskInput::allObjs,(k,v)->{
       if( k==hash ) return true;
    });
    if( data ) return data;
    return 0;
}

//find_object() - 通过文件名（file_name()）查找对象
object findObject(string fileAddr){

  var data = filterMap(TaskInput::allObjs,(k,v)->{
       if( funcExists(v,"query") && v.query("file_addr") == fileAddr ) return true;
    });
    if( data ) return data;
    return 0;
}

string fileName(object ob){

    return ob.query("file_addr");


}


var filterMap(var mapData,var func){
    var keys = keys(mapData);
    for(int i;i < keys.size();i++){
        var e = mapData[keys[i]];
        if( func(keys[i],e) ){
            return e;
        }
    }
    return 0;
}

void tellObject(object ob,string msg){
  string nid = ob.query("netAgentId");
  var channel = ob.query("netChannel");
  channel.send(nid,msg);
}

//返回所有内容对象
object *allInventory( object ob ){

}

//先查找，没有就加载并返回
var loadObject(string dest){
    var data = filterMap(TaskInput::allObjs,(k,v)->{
        // varDump("测试",v,dest);
        if( baseName(v)==dest ) return true;
    });
    if( !data ) data = new(dest);
    //只要data不是0、none false 就是true
    if( data ) return data;
    return 0;
}


int rmInventory(object env, var ob){
    string hashCode;
    if( stringp(ob) ){
        hashCode = ob;
    }

     var d = env.query("inventoryLists");
     d -= hashCode;
     return 1;
}

//void move_object( object | string dest );
//src 源对象
void moveObject( var src, var dest ){
    object ob;

   println("当前移动的Obj是"+baseName(src));
   
   

    
    if(stringp(dest)){
        ob = loadObject(dest);
    }else{
        ob = dest;
    }
//    varDump(src,dest);
    if(!ob) {//对象加载出问题
       println("对象加载错误!");
       return;
    }

    //不存实体，只存ID
    var d = ob.query("inventoryLists");
    if(!d ){
        ob.set("inventoryLists",([src]));
    }else{

    }

    println(baseName(src)+"所在当前环境" + baseName(ob));
    src.set("currEnv",ob);

}

object environment( object ob  ){
    return ob.query("currEnv") ? ob.query("currEnv") : 0;
}



var parseCommand(string msg){
    var instr,argv;
    string cmd;
    if( strsrch(msg," ") > 0 ){
        instr = msg.split(" ");
      
        //跳过第一位，取多少位
        //argv 是参数内容
         argv = subcut(instr,1,instr.size() ).join(" ");
         cmd = instr[0];
    }else{
        argv="";
        cmd = msg;
    }

  

    return ([cmd,argv]);
}

int command(object ob,string msg){

//    string cmd,argv;
     (cmd,argv) = parseCommand(msg);


    println("command 当前的指令是-->"+cmd);

     if( !TaskInput::cmdObjs.has(cmd) ){
           println("command 执行时找不到指令-->"+cmd);
         return;
     }


     TaskInput::cmdObjs[cmd].main(ob,argv);

}
